﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace Core.Public
{
    public class MD5Helper
    {
        /// <summary>
        /// Md5 32Bit 加密
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static string Md5Encipher(string text)
        {
            byte[] result = Encoding.Default.GetBytes(text);
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            return BitConverter.ToString(md5.ComputeHash(result)).Replace("-", "").ToLower();
        }

        /// <summary>
        /// Md5 16Bit 加密
        /// </summary>
        /// <param name="text">需要家庭的数据</param>
        /// <param name="isUpper">返回的是大写还是小写</param>
        /// <returns></returns>
        public string Md5Encipher16(string text, bool isUpper = true)
        {
            byte[] result = Encoding.Default.GetBytes(text);
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            string encipher = BitConverter.ToString(md5.ComputeHash(result), 4, 8).Replace("-", "");
            if (isUpper)
                return encipher.ToUpper();
            else
                return encipher.ToLower();
        }

        #region HMACSHA1 加密
        /// <summary>
        /// HMACSHA1
        /// </summary>
        /// <param name="text"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public string HMACSHA1(string text, string key)
        {
            HMACSHA1 hmacsha1 = new HMACSHA1();
            hmacsha1.Key = Encoding.UTF8.GetBytes(key);
            byte[] dataBuffer = Encoding.UTF8.GetBytes(text);
            byte[] hashBytes = hmacsha1.ComputeHash(dataBuffer);
            return Convert.ToBase64String(hashBytes);
        }
        #endregion

        #region md5salt加密
        // <summary>
        /// md5salt加密
        /// </summary>
        /// <param name="pwd">明文的密码</param>
        /// <param name="salt">密文的salt</param>
        /// <returns>密文的salt</returns>
        public static string GetPwdSail(string pwd, string salt)
        {
            return Md5Encipher(pwd + salt); 
        }
        #endregion
    }
}
